package com.glory.good_dark.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.good_dark.R;
import com.glory.good_dark.entity.ProjectModelEntity;

import java.util.List;

/**
 * FileName: ProjectModelListAdapter
 * Author: XJH
 * Date: 2020/7/20 15:22
 * Description:
 * History:
 */
public class ProjectModelListAdapter extends BaseQuickAdapter<ProjectModelEntity.ProjectInfoEntity, BaseViewHolder> {

    public ProjectModelListAdapter(@Nullable List<ProjectModelEntity.ProjectInfoEntity> data) {
        super(R.layout.adapter_project_model, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ProjectModelEntity.ProjectInfoEntity item) {
        helper.setText(R.id.tvTitle,item.getINPUTNAME());
        EditText etContent = helper.getView(R.id.etContent);
        etContent.setText(item.getDATAVALUE());
        LinearLayout llCheck = helper.getView(R.id.llCheck);
        etContent.setVisibility(item.getDATATYPE().equals("1") ? View.GONE : View.VISIBLE);
        llCheck.setVisibility(item.getDATATYPE().equals("1") ? View.VISIBLE : View.GONE);
        RadioButton rbOK = helper.getView(R.id.rbOK);
        RadioButton rbNG = helper.getView(R.id.rbNG);
        if (item.getDATAVALUE().equals("OK")){
            rbOK.setChecked(true);
        }else if (item.getDATAVALUE().equals("NG")){
            rbNG.setChecked(true);
        }
        rbOK.setOnClickListener(v -> item.setDATAVALUE("OK"));
        rbNG.setOnClickListener(v -> item.setDATAVALUE("NG"));
        setEditListener(etContent,item);
    }

    private void setEditListener(EditText editText,final ProjectModelEntity.ProjectInfoEntity item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setDATAVALUE(s.toString());
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
